package com.huawei.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Code13 {
    public static void main(String[] args) {
        System.out.println(new Code13().romanToInt("IV"));
        System.out.println(new Code13().romanToInt("II"));
        System.out.println(new Code13().romanToInt("MCMXCIV"));
        System.out.println(new Code13().romanToInt("LVIII"));
    }
    public int romanToInt(String s) {
        char[] chars = s.toCharArray();
        int index = 0;
        int ans = 0;
        Map<Character,Integer> hashTable = new HashMap<>();
        hashTable.put('I',1);
        hashTable.put('V',5);
        hashTable.put('X',10);
        hashTable.put('L',50);
        hashTable.put('C',100);
        hashTable.put('D',500);
        hashTable.put('M',1000);
        while (true){
            if(index >= chars.length-1){
                if(index == chars.length-1){
                    ans+= hashTable.get(chars[index]);
                }
                break;
            }
            char indexChar = chars[index];
            if(Objects.equals(indexChar,'I') && Objects.equals(chars[index+1],'V')){
                ans+=4;
                index++;
            }else if(Objects.equals(indexChar,'I') && Objects.equals(chars[index+1],'X')){
                ans+=9;
                index++;
            }else if(Objects.equals(indexChar,'X') && Objects.equals(chars[index+1],'L')){
                ans+=40;
                index++;
            }else if(Objects.equals(indexChar,'X') && Objects.equals(chars[index+1],'C')){
                ans+=90;
                index++;
            }else if(Objects.equals(indexChar,'C') && Objects.equals(chars[index+1],'D')){
                ans+=400;
                index++;
            }else if(Objects.equals(indexChar,'C') && Objects.equals(chars[index+1],'M')){
                ans+=900;
                index++;
            }else {
                ans+=hashTable.get(chars[index]);
            }
            index++;
        }
        return ans;
    }
}
